#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time    : 2021/7/21 13:57
# @Author  : Samge
###
# 日期相关的工具
# ###

import datetime
import time


def get_curr_time_str():
    """获取时间戳"""
    return str(time.time()).replace('.', '')[0:13]


def remove_date_point(_date):
    """去除字符串格式日期的小数点后面内容
    如：2017-06-07 21:39:34.352885  ==》2017-06-07 21:39:34
    """
    if _date is None:
        return None
    if type(_date) is datetime.datetime:
        return _date.strftime('%Y-%m-%d %H:%M:%S')
    if '.' in _date:
        return _date.split('.')[0]
    else:
        return _date


def date_interval_list(date_start=None, date_end=None):
    """获取两个日期之间的 日期数组"""
    if date_start is None:
        date_start = '2000-01-01'
    if date_end is None:
        date_end = datetime.datetime.now().strftime('%Y-%m-%d')

    date_start = datetime.datetime.strptime(date_start, '%Y-%m-%d')
    date_end = datetime.datetime.strptime(date_end, '%Y-%m-%d')
    date_list = []
    date_list.append(date_start.strftime('%Y-%m-%d'))
    while date_start < date_end:
        date_start += datetime.timedelta(days=+1)  # 日期加一天
        date_list.append(date_start.strftime('%Y-%m-%d'))  # 日期存入列表
    return date_list


def date_interval_list_desc(date_start=None, date_end=None):
    """获取两个日期之间的 日期数组"""
    if date_start is None:
        date_start = '2000-01-01'
    if date_end is None:
        date_end = datetime.datetime.now().strftime('%Y-%m-%d')

    date_start = datetime.datetime.strptime(date_start, '%Y-%m-%d')
    date_end = datetime.datetime.strptime(date_end, '%Y-%m-%d')
    date_list = []
    date_list.append(date_end.strftime('%Y-%m-%d'))
    while date_start < date_end:
        date_end -= datetime.timedelta(days=1)
        date_list.append(date_end.strftime('%Y-%m-%d'))  # 日期存入列表
    return date_list


def get_date_interval(days, format_str=None):
    """
    获取今天向前/向后 「days」 天的日期
    :format_str 时间样式，默认 %Y-%m-%d
    """
    _date = datetime.datetime.now()
    if days < 0:
        _date -= datetime.timedelta(days=abs(days))
    else:
        _date += datetime.timedelta(days=abs(days))
    if format_str is None:
        format_str = '%Y-%m-%d'
    return _date.strftime(format_str)


def is_today(c_date):
    """判断日期是否今天"""
    return datetime.datetime.now().strftime('%Y-%m-%d') == c_date


def is_today_left(c_date):
    """判断日期是否在今天之前，包含今天"""
    try:
        return (datetime.datetime.strptime(c_date[:10], '%Y-%m-%d') - datetime.datetime.now()).days <= 0
    except:
        return False


def is_today_right(c_date):
    """判断日期是否在今天之后，不包含今天"""
    try:
        return (datetime.datetime.strptime(c_date[:10], '%Y-%m-%d') - datetime.datetime.now()).days > 0
    except:
        return False


def is_date_interval(c_date, days):
    """
    判断日期是否在今天前后多少天的范围内
    @days :
            days包含 +- 符号：判断c_date 是否在 [date_now-days : date_now+days] 之间
            days < 0 ：判断在c_date 是否在 [date_now-days : date_now] 之间
            days > 0 ：判断在c_date 是否在 [date_now : date_now+days] 之间
    """
    is_double = False
    check_date = datetime.datetime.strptime(c_date, '%Y-%m-%d')
    date_now = datetime.datetime.strptime(datetime.datetime.now().strftime('%Y-%m-%d'), '%Y-%m-%d')
    if type(days) is str and '+-' in days:
        is_double = True
        days = int(days.replace('+-', ''))

    if type(days) is str:
        raise ValueError('days参数传入异常: "{}" , 如果传入的days是字符类型，必须用格式 "+-日期间隔数量", 否则用正负int类型'.format(days))

    days_interval = datetime.timedelta(days=abs(days))
    date_start = date_now - days_interval
    date_end = date_now + days_interval

    if is_double:
        return date_start <= check_date <= date_end
    elif days < 0:
        return date_start <= check_date <= date_now
    else:
        return date_now <= check_date <= date_end


def date_to_str(date, format="%Y-%m-%d %H:%M:%S"):
    """
    将时间戳转为指定字符串格式
    如：
        date_to_str(1515463620) => 2018-01-09 10:07:00
        date_to_str(1515463620, '%Y-%m-%d') => 2018-01-09
    """
    return time.strftime(format, time.localtime(date))


def get_today_str(format='%Y-%m-%d %H:%M:%S'):
    """
    获取当前年月日字符串
    :param format:
    :return:
    """
    return time.strftime(format, time.localtime(time.time()))
